package com.example.touristassist;

import com.example.touristassist.R;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MapActivity extends Activity {
	GoogleMap map;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map);

		MapFragment f = (MapFragment)getFragmentManager().findFragmentById(R.id.map);
		map = f.getMap();
		map.setMyLocationEnabled(true);

		Bundle extra = getIntent().getExtras();
		String[] a = extra.getStringArray("location");
		
		if (a.length > 0) {
			for(int i = 0; i < a.length; i++) {
				String[] s = a[i].split(";");
				
				if (s.length == 3) {
					addMarker(new LatLng(Double.valueOf(s[1]),Double.valueOf(s[2])),s[0]);
					
					if (i == 0)
						zoomMap(Double.valueOf(s[1]),Double.valueOf(s[2]));
				}
			}
		}
	}
	
	void zoomMap(double lat, double lng) {
		LatLng latLng = new LatLng(lat, lng);
	    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 17);
	    map.animateCamera(cameraUpdate);
	}
	
	private void addMarker(LatLng position, String title) {
		MarkerOptions marker = new MarkerOptions()
		.position(position)
		.title(title)
		.draggable(true)
		.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_pin2));
		map.addMarker(marker);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.map, menu);
		return true;
	}

}
